package com.study.demo.aop.demo1;

import com.study.demo.aop.util.RandomUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.util.Random;

/**
 * 如果将注解放在接口上, 实现类上不放任何注解, 也是可以生效的
 * @author: elvin
 * @time: 2019-05-23 11:02
 * @desc:
 **/
public interface IUserService {

    @Cacheable(value = "myCache", key = "'getDefault'")
    public User getDefault();

    //CacheEvict 是用来清除缓存的,
    // beforeInvocation = true 表示在这个方法之前就清除缓存,
    // beforeInvocation = false 表示在这个方法之后才清除缓存
    @CacheEvict(value = "myCache", key = "'getById_' + #id", beforeInvocation = false)
    public void clearBy(int id);

    @Cacheable(value = "myCache", key = "'getById_' + #id")
    public User getById(int id) ;

    //CachePut 是用来更新缓存的, 并不会检查缓存是否存在
    @CachePut(value = "myCache", key = "'getById_' + #id")
    public User getByIdPut(int id);
}
